﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using volva.Models;

namespace volva.Services
{

    public class ScheduleService
    {      
        /// <summary>
        /// This function adds a video to schedule.
        /// </summary>
        /// <param name="ShowID">What video to show.</param>
        /// <param name="dateNew">At what time the video should be shown.</param>
        static public void Add(int ShowID, DateTime dateNew)
        {
            ScheduleItem scheduleItem = new ScheduleItem();
            scheduleItem.Video_id = ShowID;
            scheduleItem.airDateTime = dateNew;
            scheduleItem.Station_id = StationService.GetVideoByID(ShowID).Station_id;

            DataService.ddc.ScheduleItems.InsertOnSubmit(scheduleItem);
            DataService.ddc.SubmitChanges();
        }

        /// <summary>
        /// This function deletes a show from schedule.
        /// </summary>
        /// <param name="showID">Which show to delete.</param>
        /// <param name="dateNew">At what time was the video supposed to be.</param>
        static public void Delete(int showID, DateTime dateNew)
        {
            ScheduleItem showToDelete = (from v in DataService.ddc.ScheduleItems
                                        where v.id == showID && v.airDateTime == dateNew
                                        select v).SingleOrDefault();

            DataService.ddc.ScheduleItems.DeleteOnSubmit(showToDelete);
            DataService.ddc.SubmitChanges();  
        }

        /// <summary>
        /// This function changes the show time of a video.
        /// </summary>
        /// <param name="showID">Which show to move.</param>
        /// <param name="dateOld">What time was it supposed to be shown at.</param>
        /// <param name="dateNew">At what time is it supposed to be shown at.</param>
        static public void Change(int showID, DateTime dateOld, DateTime dateNew)
        {
            ScheduleItem showToChange = (from v in DataService.ddc.ScheduleItems
                                        where v.id == showID && v.airDateTime == dateOld
                                        select v).SingleOrDefault();

            showToChange.airDateTime = dateNew;
            DataService.ddc.SubmitChanges();

        }
        static public List<ScheduleItem> GetTodaysSchedule(int stationId, DateTime day)
        {
            DateTime tomorrow = day.AddDays(1);

            List<ScheduleItem> shows =
                (from si in DataService.ddc.ScheduleItems
                 where si.Station_id == stationId && si.airDateTime >= day && si.airDateTime < tomorrow
                 orderby si.airDateTime ascending
                 select si).ToList();

            return shows;
        }
        static public List<ScheduleItem> GetAvalibleShowTimes(int stationId, DateTime day)
        {
            List<ScheduleItem> avalibleShowTimes = new List<ScheduleItem>();
            List<ScheduleItem> showTimes = ScheduleService.GetTodaysSchedule(stationId, day);
            List<DateTime?> showTimesDate = new List<DateTime?>();

            for (int i = 0; i < showTimes.Count; i++)
	        {
		        showTimesDate.Add(showTimes[i].airDateTime);
	        }

            for (int i = 0; i < 24; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    DateTime time = day;
                    ScheduleItem item = new ScheduleItem();

                    time = time.AddHours(i);
                    time = time.AddMinutes(j * 15);

                    item.airDateTime = time;

                    if (!showTimesDate.Contains(time))
                    {
                        avalibleShowTimes.Add(item);
                    }
                }
            }

            return avalibleShowTimes;
        }
    }
}
